<template>
  <div>
    <page-list :x-uri="uri" :custom-components="custom" :doactions="doactions">
      <!-- 自定义tab -->
      <template v-slot:page-tabs>
        <edu-page-tabs :tab-list="xTabs" />
      </template>
      <!-- 页面操作按钮 -->
      <template v-slot:page-button>
        <el-button v-auth="'course.agent.course'" size="mini" type="primary" @click="create">添加课程</el-button>
      </template>
    </page-list>

    <!--  机构代理课程弹窗  -->
    <choose-course v-if="courseVisible" :sid.sync="sid" :visible.sync="courseVisible" @submit="saveSchoolCourse" />
  </div>
</template>
<script>
import PageList from '@public/template/page_list.vue';
import Titile from './components/title.vue';
import Price from './components/price.vue';
import CostPrice from './components/cost_price.vue';
import EduPageTabs from '@public/template/tabs/page_tabs.vue';
import ChooseCourse from './components/choose_course.vue';

export default {
  components: {
    PageList,
    EduPageTabs,
    ChooseCourse
  },
  inject: ['reload'],
  data() {
    return {
      uri: 'course/admin/agent/' + this.$route.params.id + '/course',
      xTabs: [
        {
          title: '已审',
          uri: '/school/index',
          meta: {
            permission: 'school.list'
          }
        },
        {
          title: '课程列表',
          uri: '/course/agent/' + this.$route.params.id + '/course',
          meta: {
            permission: 'course.agent.course'
          }
        }
      ],
      custom: {
        course_title: Titile,
        price: Price,
        cost_price: CostPrice
      },
      courseVisible: false, // 课程弹窗
      sid: 0,
      doactions: [
        {
          title: '删除',
          meta: { permission: 'course.agent.course' },
          callback: this.delete
        }
      ]
    };
  },
  methods: {
    // 添加课程
    create() {
      this.sid = this.$route.params.id;
      this.courseVisible = true;
    },
    // 机构添加代理课程
    saveSchoolCourse(courseIds, courseList, term) {
      this.$http.post(
          '/course/admin/agent/school/' + this.sid + '/addCourse',
          {
            course_ids: courseIds,
            term: term
          },
          response => {
            this.$message.success(response.msg, _ => {
              this.visible = false
              this.reload();
            });
          },
          error => {
            this.$message.error(error.msg);
          }
      );
    },
    // 删除
    delete: function({ row }) {
      this.$box
          .confirm('确认删除吗')
          .then(() => {
            this.$http.delete(
                '/course/admin/agent/' + row.id + '/delete',
                {},
                response => {
                  this.$message.success(response.msg, _ => {
                    this.reload();
                  });
                },
                error => {
                  this.$message.error(error.msg);
                }
            );
          })
          .catch(() => {});
    }
  }
};
</script>
